package hc.group;

import java.util.Arrays;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RestController;

import jm.easyconfig.CfgContextEnvironmentAware;
import jm.easyconfig.CfgHelper;
import jm.easyconfig.EasyValue;

@RestController
public class CodeAccessPropsController {

	@Autowired
	CfgContextEnvironmentAware aware;

	public static class PropSample {
		@EasyValue("${complex.beanlist}")
		DataBeanSample[] beanList;
	}

	void accessByBean() {
		PropSample prop = new PropSample();

		aware.register(prop);

		System.out.println("accessByBean:" + Arrays.asList(prop.beanList));
	}

	@Autowired
	Environment environment;

	void accessByEnvironment() {
		String beanListText = environment.getProperty("complex.beanlist");

		DataBeanSample[] beanList = CfgHelper.jsonRead(beanListText, DataBeanSample[].class);

		System.out.println("accessByEnvironment:" + Arrays.asList(beanList));
	}

	@PostConstruct
	void init() {
		accessByEnvironment();

		accessByBean();
	}

}
